// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin:false,//判断状态
    src:'',//头像
    nickName:'未登录',
    carte:[],
    num:'0'
  },
  //方法


  //登录
  getMyInfo:function(e){
    let that = this;
    // console.log(e,'ee')
    let info = e.detail.userInfo
    let signature = e.detail.signature
    let avatarUrl = info.avatarUrl
    let nickName = info.nickName
    console.log(nickName)
    that.setData({
      isLogin: true, //确认登陆状态
      src: avatarUrl,   //更新图片来源
      nickName: nickName
    })
    let datas ={
      isLogin: true,
      src: avatarUrl,
      nickName:nickName
    }
    let status = that.data.isLogin == true ? '1' : '0'
    console.log(status,'status')
    wx.setStorageSync(status, datas); //存头像和昵称
  },
  // 获取收藏列表的数据
  getMyFavorites:function(){
    let that = this;
    let info = wx.getStorageInfoSync();//读取本地缓存信息
    console.log(info,'info')
    console.log(info.keys,'原来')
    let arrnum = info.keys;
    arrnum.shift()
    arrnum.pop();
    let num = arrnum.length//条数

    let datalist=[]
    for (let i = 0; i < arrnum.length;i++){
      let obj = wx.getStorageSync(arrnum[i]);
      // console.log(obj,'123132')
      datalist.push(obj);//将新闻添加到数组中
    }
    // console.log(datalist,'datalist')
    that.setData({
      carte: datalist,
      num: num
    })
  },
  jump: function (e) {
    let that = this;
    console.log(e, 'e')
    let date = {}
    date.name = e.currentTarget.dataset.name
    date.id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../details/details?date=' + JSON.stringify(date)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    var status = wx.getStorageSync('1');
    if (status.isLogin){
      console.log('登录成功')
      that.getMyFavorites();
      that.setData({
        isLogin: status.isLogin, //确认登陆状态
        src: status.src,   //更新图片来源
        nickName: status.nickName
      })
    }else{
      console.log('未登录')
    }
    
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let status = that.data.isLogin
    if (status){
      this.getMyFavorites();
    }else{
      console.log('去登录')
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})